import axios from 'axios'
import Bus from '../bus/bus'

// const bas = process.env.NODE_ENV === 'development' ? 'http://localhost:8000' : 'http://118.89.227.78:81'
// let base = process.env.API_ROOT
let bas = 'http://118.89.227.78:81'
// let bas = 'http://localhost:8000'

let base = `${bas}/api`

// let base = '/api'

function createInstance () {
  let instance = axios.create({
    baseURL: base
  })
  instance.defaults.headers.common['X-XSRF-TOKEN'] = `${localStorage.getItem('token')}`
  return instance
}

function responseFilter (response) {
  // console.log('111', response)
  let {status, data} = response
  if (status >= 200 && status < 300) {
    return data
  }

  switch (status) {
    case 401: {
      localStorage.removeItem('token')
      // pageJump("/login")
      window.alert('请登录')
      return Promise.reject()
    }
    default: {
      return Promise.reject(data.message)
    }
  }
}

function responseCatchError (msg) {
  switch (msg.response.status) {
    case 401:
      localStorage.removeItem('token')
      Bus.$emit('logIn')
      return
  }
}

let loginInstance = axios.create({
  baseURL: base
})

export const GetCarousel = params => {
  return loginInstance.get(`/carousel`).then(responseFilter).catch(responseCatchError)
}

// 登陆接口
export const UserLogin = params => {
  return loginInstance.post(`/user/login`, params).then(responseFilter).catch(responseCatchError)
}

export const UserLogout = params => {
  return createInstance().post(`/user/logout`, params).then(responseFilter).catch(responseCatchError)
}

export const UserInfoRead = params => {
  return createInstance().get(`/user/info/read`, {params: params}).then(responseFilter).catch(responseCatchError)
}
export const UserInfoReadMyself = params => {
  return createInstance().get(`/user/info/read/myself`, {params: params}).then(responseFilter).catch(responseCatchError)
}

export const UserInfoUpdate = params => {
  return createInstance().post(`/user/info/update`, params).then(responseFilter).catch(responseCatchError)
}

export const UserRegister = params => {
  return createInstance().post(`/user/register`, params).then(responseFilter).catch(responseCatchError)
}
export const ResetPassword = params => {
  return createInstance().put(`/user/resetPassword`, params).then(responseFilter).catch(responseCatchError)
}
export const ConfirmEmail = params => {
  return createInstance().get(`/email`, {params: params}).then(responseFilter).catch(responseCatchError)
}

export const SetPassword = params => {
  return createInstance().put(`/user/setPassword`, params).then(responseFilter).catch(responseCatchError)
}

// 首页
/* 获取所有post */
export const getAllPost = params => {
  return createInstance().get(`/post/readall`, {params: params}).then(responseFilter).catch(responseCatchError)
}

/* 获取热门post */
export const getHotPost = params => {
  return createInstance().get(`/post/hot`, {params: params}).then(responseFilter).catch(responseCatchError)
}

/* 添加post */
export const addPost2 = params => {
  return createInstance().post(`/post/add`, params).then(responseFilter).catch(responseCatchError)
}

export const addPost = params => {
  return createInstance().post(`/post/add`, params).then(responseFilter).catch(responseCatchError)
}

export const RemoveImage = params => {
  return createInstance().post(`/post/img/delete`, params).then(responseFilter).catch(responseCatchError)
}

/* 获得post */
export const getPost = params => {
  return createInstance().post(`/post/read`, params).then(responseFilter).catch(responseCatchError)
}

export const delPost = params => {
  return createInstance().put(`/post/delete`, params).then(responseFilter).catch(responseCatchError)
}

export const changeStatus = params => {
  return createInstance().put(`/post/status`, params).then(responseFilter).catch(responseCatchError)
}

// 点赞
export const voteRead = params => {
  return createInstance().get(`/vote/read`, {params: params}).then(responseFilter).catch(responseCatchError)
}
export const voteAdd = (params, url) => {
  return createInstance().post(`/vote/${url}`, params).then(responseFilter).catch(responseCatchError)
}

// 收藏
export const collectRead = params => {
  return createInstance().get(`/collect/read`, {params: params}).then(responseFilter).catch(responseCatchError)
}

export const collectOption = (params, url) => {
  return createInstance().post(`/collect/${url}`, params).then(responseFilter).catch(responseCatchError)
}
// 评论
export const commentAdd = params => {
  return createInstance().post(`/comment/add`, params).then(responseFilter).catch(responseCatchError)
}
export const commentRemove = params => {
  return createInstance().post(`/comment/delete`, params).then(responseFilter).catch(responseCatchError)
}
export const commentRead = params => {
  return createInstance().post(`/comment/read`, params).then(responseFilter).catch(responseCatchError)
}

// 朋友
export const friendAdd = params => {
  return createInstance().post(`/friend/add`, params).then(responseFilter).catch(responseCatchError)
}
export const friendAgree = params => {
  return createInstance().post(`/friend/agree`, params).then(responseFilter).catch(responseCatchError)
}
export const friendRefuse = params => {
  return createInstance().post(`/friend/refuse`, params).then(responseFilter).catch(responseCatchError)
}

export const friendSearch = params => {
  return createInstance().get(`/friend/search`, {params: params}).then(responseFilter).catch(responseCatchError)
}
export const friendApplyRead = params => {
  return createInstance().get(`/friend/read`, params).then(responseFilter).catch(responseCatchError)
}
export const friendListRead = params => {
  return createInstance().get(`/friend/list`, {params: params}).then(responseFilter).catch(responseCatchError)
}
export const friendRemove = params => {
  return createInstance().post(`/friend/delete`, params).then(responseFilter).catch(responseCatchError)
}

// 群组
export const groupRead = params => {
  return createInstance().get(`/group/list`, {params: params}).then(responseFilter).catch(responseCatchError)
}
export const groupQuit = params => {
  return createInstance().post(`/group/quit`, params).then(responseFilter).catch(responseCatchError)
}
export const groupInvite = params => {
  return createInstance().post(`/group/invite`, params).then(responseFilter).catch(responseCatchError)
}
export const groupSearch = params => {
  return createInstance().post(`/group/search`, params).then(responseFilter).catch(responseCatchError)
}
export const groupInviteRead = params => {
  return createInstance().get(`/group/invite/read`, {params: params}).then(responseFilter).catch(responseCatchError)
}
export const groupJoin = params => {
  return createInstance().post(`/group/join`, params).then(responseFilter).catch(responseCatchError)
}
export const changeRoleApi = params => {
  return createInstance().put(`/group/status`, params).then(responseFilter).catch(responseCatchError)
}
export const groupInviteOption = (params, option) => {
  return createInstance().post(`/group/invite/${option}`, params).then(responseFilter).catch(responseCatchError)
}
export const groupCreate = params => {
  return createInstance().post(`/group/create`, params).then(responseFilter).catch(responseCatchError)
}

export const groupReadMember = params => {
  return createInstance().get(`/group/read/list`, {params: params}).then(responseFilter).catch(responseCatchError)
}
export const groupReadInfo = params => {
  return createInstance().get(`/group/read/info`, {params: params}).then(responseFilter).catch(responseCatchError)
}
export const groupInfoUpdate = params => {
  return createInstance().post(`/group/leader/update`, params).then(responseFilter).catch(responseCatchError)
}

/* 通知部分 */
export const addNotice = params => {
  return createInstance().post(`/notice/add`, params).then(responseFilter).catch(responseCatchError)
}
export const getNotice = params => {
  return createInstance().get(`/notice/read`, {params: params}).then(responseFilter).catch(responseCatchError)
}
export const updateNotice = params => {
  return createInstance().put(`/notice/update`, params).then(responseFilter).catch(responseCatchError)
}

/* 获取设置信息 */
export const getSet = params => {
  return createInstance().get(`/user/set/read/myself`).then(responseFilter).catch(responseCatchError)
}

export const aaaa = params => {
  return createInstance().post(`/ceshi/token`, params).then(responseFilter).catch(responseCatchError)
}
/* 上传照片 */
export const uploadPic = params => {
  return createInstance().post(`/file/upload`, params).then(responseFilter).catch(responseCatchError)
}

export const getImageUrl = x => {
  return `${bas}${x}`
}

// 获取推荐文章列表
export const getArticleList = params => {
  return createInstance().get(`/article`, {params: params}).then(responseFilter)
}
